//
//  MainTabBarController.swift
//  Eternity
//
//  Created by ZhengWei on 2016/11/27.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController, UITabBarControllerDelegate {
    
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initBaseLayout()
        self.initSubViewControllers()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - private method
    func initBaseLayout() {
        
        self.tabBar.tintColor = UIColor(hexString: "#252525")
        self.delegate = self
        UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(hexString: "#252525") ?? UIColor.white], for: .selected)
    }
    
    func initSubViewControllers() -> Void {
        let shopVC = ShopViewController()
        let shopNav = MainNavigationController(rootViewController: shopVC)
        self.setUpTabBarItem(viewController: shopNav, title: "SHOP", image: "tab_shop_n", selectedImage: "tab_shop_s")
        
        
        let classifyVC = ClassifyViewController()
        let classifyNav = MainNavigationController(rootViewController: classifyVC)
        classifyVC.title = "分类"
        self.setUpTabBarItem(viewController: classifyNav, title: "CATEGORIES", image: "tab_discovery_n-1", selectedImage: "tab_discovery_s-1")
        
        
        let myBagVC = MyBagViewController()
        let myBagNav = MainNavigationController(rootViewController: myBagVC)
        myBagVC.title = "化妆包"
        self.setUpTabBarItem(viewController: myBagNav, title: "MY BAG", image: "tab_mybag_n", selectedImage: "tab_mybag_s")
        
        
        let meVC = MeViewController()
        let meNav = MainNavigationController(rootViewController: meVC)
        meVC.title = "个人中心"
        self.setUpTabBarItem(viewController: meNav, title: "ME", image: "tab_me_n", selectedImage: "tab_me_s")
        
        
        self.viewControllers = [shopNav, classifyNav, myBagNav, meNav]
    }
    
    func setUpTabBarItem(viewController: MainNavigationController, title: String, image: String, selectedImage: String) -> Void {
        let tabbarItem = UITabBarItem(title: title, image: UIImage(named: image), selectedImage: UIImage(named: selectedImage))
        viewController.tabBarItem = tabbarItem
    }
    
    // MARK: - setters and getters
    var _lastSelectedIndex: NSInteger!
    var lastSelectedIndex: NSInteger {
        if _lastSelectedIndex == nil {
            _lastSelectedIndex = NSInteger()
            //判断是否相等,不同才设置
            if (self.selectedIndex != selectedIndex) {
                //设置最近一次
                _lastSelectedIndex = self.selectedIndex;
            }
            //调用父类的setSelectedIndex
            super.selectedIndex = selectedIndex
        }
        return _lastSelectedIndex
    }
    
    // MARK: - UITabBarControllerDelegate
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        
        if viewController == self.viewControllers![3]  {
            
            if !defaultUserInfo.isLogin() {
            
                self.selectedIndex = lastSelectedIndex
                let getCodeVC = GetCodeViewController()
                let nav = UINavigationController(rootViewController: getCodeVC)
                nav.navigationBar.barTintColor = UIColor(hexString: "#FFFFFF")
                self.viewControllers![selectedIndex].present(nav, animated: true, completion: nil)
                return false
            } else {
                return true
            }
        }
        return true
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        //获取选中的item
        let tabIndex = tabBar.items?.index(of: item)
        if tabIndex != self.selectedIndex {
            //设置最近一次变更
            _lastSelectedIndex = self.selectedIndex
        }
    }
}


